import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:provider/provider.dart';
import 'package:tiktokadult/assets_tool/images.dart';
import 'package:tiktokadult/hj_utils/holiday_theme.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';

import '../../community_business/publish_page/publish_page.dart';

enum PublishEntry {
  community('publish.webp', 40, 40);

  final String img;
  final double width;
  final double height;

  const PublishEntry(this.img, this.width, this.height);

  String realPath() {
    return HolidayTheme.theme.addPublish;
  }
}

class BackToTopButton extends StatefulWidget {
  final ScrollController controller;

  const BackToTopButton({super.key, required this.controller});

  @override
  State<BackToTopButton> createState() => _BackToTopButtonState();
}

class _BackToTopButtonState extends State<BackToTopButton> {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider.value(
      value: widget.controller,
      child: Consumer<ScrollController>(builder: (_, ctr, __) {
        if (ctr.hasClients) {
          if (ctr.offset <= 120) return SizedBox.shrink();
          return GestureDetector(
            onTap: () {
              ctr.jumpTo(0);
            },
            child: Column(
              children: [
                Image.asset(
                  'back_to_top.png'.communityPath,
                  width: 84.w,
                  height: 55.h,
                ),
                10.sizeBoxH,
              ],
            ),
          );
        }
        return SizedBox.shrink();
      }),
    );
  }
}

class PublishButton extends StatefulWidget {
  final PublishEntry entry;

  const PublishButton({super.key, this.entry = PublishEntry.community});

  @override
  State<PublishButton> createState() => _PublishButtonState();
}

class _PublishButtonState extends State<PublishButton> {
  final _key = GlobalKey();

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        Get.bottomSheet(CommunityPublishBottomSheet(), isScrollControlled: true, isDismissible: true);
      },
      child: Container(
        key: _key,
        child: Image.asset(
          widget.entry.realPath(),
          width: widget.entry.width.w,
          height: widget.entry.height.h,
        ),
      ),
    );
  }
}

class CommunityPublishBottomSheet extends StatelessWidget {
  const CommunityPublishBottomSheet({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 18.w, vertical: 18),
      decoration: BoxDecoration(color: Color(0xff202133), borderRadius: BorderRadius.vertical(top: Radius.circular(13))),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          Container(
            width: double.infinity,
            child: Stack(
              alignment: Alignment.center,
              children: [
                Text(
                  '选择发布类型',
                  style: TextStyle(color: Colors.white, fontSize: 20.sp, fontWeight: FontWeight.w900),
                ),
                Positioned(
                  right: 0,
                  child: GestureDetector(
                    onTap: () => Get.back(),
                    child: Icon(
                      Icons.close,
                      weight: 2,
                      color: Colors.white,
                      size: 20.sp,
                    ),
                  ),
                )
              ],
            ),
          ),
          40.h.sizeBoxH,
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: () {
                  Get.back();
                  Get.toNamed(PublishPage.routeName, arguments: PublishType.homeImg);
                },
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Image.asset(
                      'publish_img.webp'.communityPath,
                      width: 45.w,
                      height: 45.w,
                    ),
                    4.sizeBoxH,
                    Text(
                      '图片',
                      style: TextStyle(
                        color: Color(0xffa3a2a2),
                        fontSize: 18.sp,
                      ),
                    )
                  ],
                ),
              ),
              40.sizeBoxW,
              GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: () {
                  Get.back();
                  Get.toNamed(PublishPage.routeName, arguments: PublishType.homeVideo);
                },
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Image.asset(
                      'publish_video.webp'.communityPath,
                      width: 45.w,
                      height: 45.w,
                    ),
                    4.sizeBoxH,
                    Text(
                      '视频',
                      style: TextStyle(
                        color: Color(0xffa3a2a2),
                        fontSize: 18.sp,
                      ),
                    )
                  ],
                ),
              ),
              40.sizeBoxW,
              GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: () {
                  Get.back();
                  Get.toNamed(PublishPage.routeName, arguments: PublishType.homeImgText);
                },
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Image.asset(
                      'publish_img_text.webp'.communityPath,
                      width: 45.w,
                      height: 45.w,
                    ),
                    4.sizeBoxH,
                    Text(
                      '图文',
                      style: TextStyle(
                        color: Color(0xffa3a2a2),
                        fontSize: 18.sp,
                      ),
                    )
                  ],
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}

class PublishEntryAlert extends StatelessWidget {
  final Offset offset;

  const PublishEntryAlert({super.key, this.offset = Offset.zero});

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Positioned(
          child: Container(
            width: 100,
            height: 81,
            padding: EdgeInsets.only(left: 12, right: 10, bottom: 10, top: 10),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(10),
                bottomLeft: Radius.circular(10),
                bottomRight: Radius.circular(10),
              ),
              gradient: LinearGradient(
                  colors: [Color(0xffFFEBD9), Colors.white], stops: [0.3, 1], begin: Alignment.topCenter, end: Alignment.bottomCenter),
            ),
            child: Material(
              color: Colors.transparent,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.end,
                children: [
                  Expanded(
                    child: GestureDetector(
                      behavior: HitTestBehavior.opaque,
                      onTap: () {
                        Get.back();
                        Get.toNamed(PublishPage.routeName, arguments: PublishType.homeImgText);
                      },
                      child: Row(
                        children: [
                          Image.asset(
                            'publish_photo.png'.homePath,
                            width: 24,
                            height: 24,
                          ),
                          18.sizeBoxW,
                          Text(
                            '图文',
                            style: TextStyle(
                              color: Color(0xffF68216),
                              fontSize: 12,
                            ),
                          )
                        ],
                      ),
                    ),
                  ),
                  Container(
                    width: 49,
                    height: 1,
                    color: Colors.black.withOpacity(.1),
                  ),
                  Expanded(
                    child: GestureDetector(
                      behavior: HitTestBehavior.opaque,
                      onTap: () {
                        Get.back();
                        Get.toNamed(PublishPage.routeName, arguments: PublishType.homeVideo);
                      },
                      child: Row(
                        children: [
                          Image.asset(
                            'publish_video.webp'.homePath,
                            width: 24,
                            height: 24,
                          ),
                          18.sizeBoxW,
                          Text(
                            '视频',
                            style: TextStyle(
                              color: Color(0xffF68216),
                              fontSize: 12,
                            ),
                          )
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
          left: offset.dx - 90,
          top: offset.dy + 5,
        )
      ],
    );
  }
}
